package domain;

public class Register {
    private ProductCatalog catalog;
    private Sale currentSale;

    public Register(ProductCatalog catalog) {
        this.catalog = catalog;
        this.currentSale = null; // 默认初始化为空
    }

    public void endSale() {
        if (currentSale != null) {
            currentSale.becomeComplete(); // 完成销售
            currentSale = null; // 完成后，销售归空
        }
    }

    public void enterItem(ItemID id, int quantity) {
        if (currentSale == null) {
            throw new IllegalStateException("Sale has not started yet.");
        }
        ProductDescription desc = catalog.getProductDescription(id);
        if (desc != null) {
            currentSale.makeLineItem(desc, quantity); // 将商品和数量加入到当前销售中
        } else {
            System.out.println("Product not found for ID: " + id);
        }
    }

    public void makeNewSale() {
        this.currentSale = new Sale(); // 创建新的销售实例
    }

    public void makePayment(Money cashTendered) {
        if (currentSale == null) {
            throw new IllegalStateException("Sale has not started yet.");
        }
        currentSale.makePayment(cashTendered); // 处理支付
    }

    public Sale getCurrentSale() {
        return currentSale;
    }
}
